/**
 * @file  ctrlZee.h
 * @brief Header file that declares functions for a Z-Stack module that provides 
 *        ethernet related functionality.
 * @author Vamshi Ponnapalli
 */

#ifndef CTRL_ZEE_H
  #define CTRL_ZEE_H  
  /*****************************************************************************
   * Includes
   ****************************************************************************/
  #include "ZComDef.h"

  /*****************************************************************************
   * Data types
   ****************************************************************************/
  //CtrlZee host constants
  #define CTRLZEE_HOST_MAX_PAYLOAD_SIZE         254

  typedef struct
  {
    byte command;
    byte data[CTRLZEE_HOST_MAX_PAYLOAD_SIZE];
  } ctrlzee_host_cmd_t;

  typedef struct
  {
    byte command;
    byte arg1;
    byte arg2;
    byte arg3;
  } ctrlzee_peer_cmd_t;
  
  typedef struct
  {
    osal_event_hdr_t hdr;
    byte arg1;
    byte arg2;
  } ctrlzee_osal_msg_t;
  
  /*****************************************************************************
   * Macros
   ****************************************************************************/

  /*****************************************************************************
   * Constants
   ****************************************************************************/
  // These constants are only for example and should be changed to the
  // device's needs
  #define CTRLZEE_ENDPOINT_CONTROL              10
  
  #define CTRLZEE_PROFILE_ID                    0x0F04

  #define CTRLZEE_DEVICE_ID_CONTROL             0x0001
  #define CTRLZEE_DEVICE_ID_LIGHT               0x0002
  #define CTRLZEE_DEVICE_ID_HVAC                0x0003

  #define CTRLZEE_DEVICE_VERSION                0
  #define CTRLZEE_FLAGS_DEFAULT                 0
  
  #define CTRLZEE_MAX_CLUSTERS                  1
  
  #define CTRLZEE_CONTROL_INPUT_CLUSTERS        1
  #define CTRLZEE_CONTROL_OUTPUT_CLUSTERS       2
  #define CTRLZEE_TOTAL_CLUSTERS                3

  #define CTRLZEE_CLUSTER_ID_COMMAND            0x01
  #define CTRLZEE_CLUSTER_ID_ONOFF              0x02
  #define CTRLZEE_CLUSTER_ID_STATUS             0x03
  
  //Define a receive poll duration of 1 second.
  #define CTRLZEE_OSAL_POLL_DURATION            1000
  
  //Send ping messages every 5 seconds.
  #define CTRLZEE_OSAL_PING_DURATION            5000
  
  //Status report duration of 5 seconds.
  #define CTRLZEE_OSAL_REPORT_STATUS_DURATION   5000

  // Application Events (OSAL) - These are bit weighted definitions.
  #define CTRLZEE_OSAL_EVENT_CONNECT_HOST       0x0001
  #define CTRLZEE_OSAL_EVENT_CHECK_HOST_DATA    0x0002
  #define CTRLZEE_OSAL_EVENT_SEND_PING          0x0004
  #define CTRLZEE_OSAL_EVENT_SCAN_DEVICES       0x0008
  #define CTRLZEE_OSAL_EVENT_REPORT_STATUS      0x0010
  
  //CtrlZee host commands
  #define CTRLZEE_HOST_CMD_INIT                 0x41  // 'A'
  #define CTRLZEE_HOST_CMD_INIT_RESP            0x61  // 'a'
  #define CTRLZEE_HOST_CMD_LIST                 0x42  // 'B'
  #define CTRLZEE_HOST_CMD_LIST_RESP            0x62  // 'b'
  #define CTRLZEE_HOST_CMD_DISC                 0x43  // 'C'
  #define CTRLZEE_HOST_CMD_DISC_RESP            0x63  // 'c'  
  #define CTRLZEE_HOST_CMD_ONOFF                0x44  // 'D'
  #define CTRLZEE_HOST_CMD_ONOFF_RESP           0x64  // 'd'
  #define CTRLZEE_HOST_CMD_REPORT_ONOFF         0x45  // 'E'
  #define CTRLZEE_HOST_CMD_REPORT_ONOFF_RESP    0x65  // 'e'
  #define CTRLZEE_HOST_CMD_START_PING           0x46  // 'F'
  #define CTRLZEE_HOST_CMD_START_PING_RESP      0x66  // 'f'
  #define CTRLZEE_HOST_CMD_STOP_PING            0x47  // 'G'
  #define CTRLZEE_HOST_CMD_STOP_PING_RESP       0x67  // 'g'
  #define CTRLZEE_HOST_CMD_REPORT_DATA          0x48  // 'H' - should never be used.
  #define CTRLZEE_HOST_CMD_REPORT_DATA_RESP     0x68  // 'h'
  #define CTRLZEE_HOST_CMD_PEER_DISC            0x49  // 'I'
  #define CTRLZEE_HOST_CMD_PEER_DISC_RESP       0x69  // 'i'
  
  //CtrlZee message constants
  #define CTRLZEE_MESSAGE_HOST_HELO             "HELO"
  #define CTRLZEE_MESSAGE_HOST_HELO_SIZE         4

  //CtrlZee peer commands
  #define CTRLZEE_PEER_CMD_PING                 0x01
  #define CTRLZEE_PEER_CMD_REPORT_ONOFF         0x02
  #define CTRLZEE_PEER_CMD_REPORT_STATUS        0x03
  #define CTRLZEE_PEER_CMD_ONOFF                0x04
  #define CTRLZEE_PEER_CMD_DISCOVER             0x05

  #define CTRLZEE_PEER_CMD_ARG_ON               0x58 //ASCII for 'X'
  #define CTRLZEE_PEER_CMD_ARG_OFF              0x78 //ASCII for 'x'
  
  //CtrlZee debug modes
  #define CTRLZEE_MAX_DEBUG_LEVELS              2
  #define CTRLZEE_NO_DEBUG                      0
  #define CTRLZEE_DEBUG                         1
  
  //CtrlZee status codes.
  #define CTRLZEE_STATUS_OK                     0x00
  #define CTRLZEE_STATUS_FAILED                 0xFF
  /*****************************************************************************
   * Function declarations
   ****************************************************************************/
  /*
   * Task Initialization function for the ctrlZee control endpoint module.
   * @param taskId The id of the OSAL task associated with this module.
   */
  extern void ctrlZee_controlInit(byte taskId);

  /*
   * OSAL task event processor for the ctrlZee control endpoint module.
   *
   * @param taskId The id of the task that the event was addressed to.
   * @param events Event flags associated with the event.
   * 
   * @return Any unprocessed event flags.
   */
  extern UINT16 ctrlZee_controlProcessEvent(byte taskId, UINT16 events);

  /*
   * Task Initialization function for the ctrlZee light endpoint module.
   * @param taskId The id of the OSAL task associated with this module.
   */
  extern void ctrlZee_lightInit(byte taskId);

  /*
   * OSAL task event processor for the ctrlZee light endpoint module.
   *
   * @param taskId The id of the task that the event was addressed to.
   * @param events Event flags associated with the event.
   * 
   * @return Any unprocessed event flags.
   */
  extern UINT16 ctrlZee_lightProcessEvent(byte taskId, UINT16 events);

  /**
   * Registers a light endpoint with the control. This allows the control to use
   * OSAL messages to communicate with the endpoint.
   *
   * @param taskId The id of hte task that is associated with the endpoint.
   *
   * @return The task id of the control endpoint. 
   */
  extern byte ctrlZee_registerLightEndpoint(byte taskId);
          
#endif
